package com.xuexiang.xuidemo.page.text;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import com.xuexiang.xui_lib.component.textview.autoFit.AutoFitHelper;
import com.xuexiang.xui_lib.component.textview.autoFit.AutoFitLayout;
import com.xuexiang.xui_lib.component.textview.autoFit.AutoFitTextView;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;

import java.util.Random;

/**
 * AutoFitText 自适应字体大小的文字
 *
 * @since 2021-04-06
 */
@Page(name = "AutoFitTextView", subTitle = "自适应字体大小的文字",
    action = "AutoFitTextAbilitySlice", ability = TextAbility.class)
public class AutoFitTextAbilitySlice extends BasePageAbilitySlice implements Component.ClickedListener {
    StringBuilder sb = new StringBuilder();
    private AutoFitTextView sliceAutofitAftv;
    private Text sliceAutofitTv, sliceTvInAutoFit;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_slice_autofit_text;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        findComponentById(ResourceTable.Id_slice_autofit_addBtn).setClickedListener(this);
        sliceAutofitAftv = (AutoFitTextView) findComponentById(ResourceTable.Id_slice_autofit_aftv);
        sliceAutofitTv = (Text) findComponentById(ResourceTable.Id_slice_autofit_tv);
        sliceTvInAutoFit = (Text) findComponentById(ResourceTable.Id_slice_tv_in_auto_fit);

//        AutoFitLayout autoFitLayout = (AutoFitLayout) findCastComponentById(ResourceTable.Id_slice_autoFitLayout);
//        Text textView = (Text) autoFitLayout.getComponentAt(0);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_slice_autofit_addBtn:
                sb.append(getCharAndNumr(2));
                setText(sb);
                break;
            default:
                break;
        }
    }

    private void setText(StringBuilder sb) {
        sliceAutofitAftv.setText(sb.toString());
        sliceAutofitTv.setText(sb.toString());
        sliceTvInAutoFit.setText(sb.toString());
    }

    public static String getCharAndNumr(int length) {
        Random random = new Random();

        StringBuffer valSb = new StringBuffer();

        String charStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

        int charLength = charStr.length();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(charLength);
            valSb.append(charStr.charAt(index));
        }

        return valSb.toString();
    }
}
